package resolver

// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.

import (
	"context"
	"errors"
	"fmt"
	"strings"

	"mall/service/blog/rpc/blogclient"
	"mall/service/wpgql/api/internal/graph/helper"
	"mall/service/wpgql/api/internal/graph/model"
	"mall/service/wpgql/api/internal/graph/util"
)

func (r *queryResolver) ContentNode(
	ctx context.Context,
	id string,
	idType *model.ContentNodeIDTypeEnum,
	contentType *model.ContentTypeEnum,
	asPreview *bool,
) (model.ContentNode, error) {
	rpcEntityName, filter := helper.GetBlogContentNodeFilter(id, idType, contentType)
	if rpcEntityName == util.RpcEntityNames.BlogPost {
		req := &blogclient.PostDetailRequest{}
		if filter != "" {
			req.Filter = filter
		}

		var preview bool
		if asPreview != nil {
			preview = *asPreview
		}
		req.AsPreview = preview

		preload, eagerLoad := helper.GetBlogContentNodePreloads(ctx)
		if preload != "" {
			req.Preload = strings.Replace(preload, util.RpcEntityNames.BlogContentNode, util.RpcEntityNames.BlogPost, -1)
		}
		if eagerLoad != "" {
			req.EagerLoad = strings.Replace(eagerLoad, util.RpcEntityNames.BlogContentNode, util.RpcEntityNames.BlogPost, -1)
		}

		res, err := r.svcCtx.BlogRpc.PostDetail(ctx, req)
		if err != nil {
			return nil, errors.New(blogPostSingleError)
		}

		post := helper.BlogPostToGraphQL(res)
		if post != nil {
			if resp, ok := interface{}(*post).(model.ContentNode); ok {
				return resp, nil
			}
		}
	}

	return nil, nil
}

func (r *queryResolver) ContentNodes(
	ctx context.Context,
	first *int,
	last *int, after *string, before *string, where *model.RootQueryToContentNodeConnectionWhereArgs) (*model.RootQueryToContentNodeConnection, error) {
	panic(fmt.Errorf("not implemented"))
}
